<?php
/**
 * Created by PhpStorm.
 * User: 64423
 * Date: 2018/10/10
 * Time: 11:26
 */

namespace app\shop\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\shop\model\BrandModel;
use app\shop\model\GoodsCategoryModel;
use app\shop\model\GoodsModel;
use app\shop\model\GoodsSpecModel;

class Goods extends Admin
{
    public function index()
    {
        $list = GoodsModel::getList();
        $page = $list->render();
        $rows = [
            ['id', 'ID'],
            ['name','商品名称'],
            ['cate_name','分类名称'],
            ['brand_name','品牌名称'],
            ['store_num','库存','text.edit'],
            ['sort','排序','text.edit'],
            ['is_sell','上架','switch'],
            ['is_hot','热销','switch'],
            ['is_rec','推荐','switch'],
            ['is_new','新品','switch'],
            ['right_button','操作']
        ];
        return ZBuilder::make('table')
            ->setPageTitle('商品列表')
            ->setTableName('goods')
            ->addColumns($rows)
            ->addTopButton('add')
            ->addRightButtons('edit,delete')
            ->setPages($page)
            ->setRowList($list)
            ->fetch();
    }

    public function add()
    {
        if(request()->isPost()){
            $this->store();
        }
        $brand_list = BrandModel::getList();
        $cate_list = categoryTree(GoodsCategoryModel::getList());
        $this->assign('brand_list', $brand_list);
        $this->assign('cate_list', $cate_list);
        return $this->fetch();
    }

    public function edit($id = 0)
    {
        if(request()->isPost()){
            $this->store();
        }
        $goods_info = GoodsModel::getOne($id);
        $goods_info['original_img_list'] = explode(',',$goods_info['original_img']);
        $brand_list = BrandModel::getList();
        $cate_list = categoryTree(GoodsCategoryModel::getList());
        $this->assign('brand_list', $brand_list);
        $this->assign('cate_list', $cate_list);
        $this->assign('goods_info', $goods_info);

        return $this->fetch();

    }

    public function delete($ids=0)
    {
        if($ids == 0){
            $this->error('参数错误');
        }
        GoodsModel::del($ids);
        GoodsSpecModel::delByGoodsId($ids);
        $this->success('删除成功');
    }

    private function store()
    {
        $data = input('post.');
        $id = input('param.id',0);
        $validate = new \app\shop\validate\Goods();
        if(!$validate->check($data)){
            $this->error($validate->getError());
        }
        $data['is_sell'] = intval(isset($data['is_sell']));
        $data['is_hot'] = intval(isset($data['is_hot']));
        $data['is_rec'] = intval(isset($data['is_rec']));
        $data['is_new'] = intval(isset($data['is_new']));
        $data['virtual'] = intval(isset($data['virtual']));
        if($id == 0){
            // 保存商品基本信息
            $goods_id = GoodsModel::store($data, $id);
            // 保存规格
            $this->saveSpec($data, $goods_id);
            $this->success('保存成功',url('index'));
        }else{
            // 保存商品基本信息
            GoodsModel::store($data, $id);
            // 保存规格
            $this->saveSpec($data, $id);
            $this->success('保存成功','index');
        }
    }



    private function saveSpec($data, $goods_id)
    {
        if($data['spec_name'] && count($data['spec_name']) > 0){
            if(count($data['spec_name']) == 1 && $data['spec_name'][0] == ''){
                return false;
            }
            $spec = [];
            $i = 1;
            foreach ($data['spec_name'] as $key => $item){
                $it = [
                    'name' => $item,
                    'price' => $data['spec_price'][$key],
                    'goods_id' => $goods_id,
                    'key' => $goods_id.'_'.$i
                ];
                array_push($spec,$it);
                $i++;
            }
            GoodsSpecModel::saveByGoodsId($goods_id,$spec);
        }
    }

}